// components/calendar/calendar.js
Component({
    /**
     * 组件的属性列表
     */
	properties: {
		currentYear: { // 当前显示的年
			type: Number,
			value: new Date().getFullYear()
		},
		currentMonth: { // // 当前显示的月
			type: Number,
			value: new Date().getMonth() + 1
		},
		multi:{ // 单选/多选
			type:Boolean,
			Value:false
		}
	},

    /**
     * 组件的初始数据
     */
	data: {
		currentMonthDateLen: 0, // 当月天数
		preMonthDateLen: 0, // 当月中，上月多余天数
		allArr:[], // 当月所有数据
		newAllArr:[], //当月所有数据
        selectDay:{}, // 选中数据,
		isShow:false
	},
	ready(){
		this.getAllArr()
	},

    /**
     * 组件的方法列表
     */
	methods: {
		close(){
			console.log("关闭")
			this.setData({
				isShow:false
			})
		},
		open(){
			this.setData({
				isShow:true
			})
		},
		//确定
		confirm(){
			console.log(this.data.newAllArr, '2313')
			let date=[]
			if(this.data.multi){
				const arr=this.data.newAllArr
	
				let res = arr.filter((item) => {
					return item.choosed===true
				  });
				  for(let i=0;i<res.length;i++){
					const item=res[i]
					date.push(`${item.year}-${item.currentMonth}-${item.date}`)
				  }
			}else{
			
				Object.keys(this.data.selectDay).length===0?[]:date.push(`${this.data.selectDay.year}-${this.data.selectDay.month}-${this.data.selectDay.day}`)
			}

			console.log(date,'res')
			this.triggerEvent('send', date)
			this.setData({
                isShow: false
	
            })
		},
		// 获取某年某月总共多少天
		getDateLen(year, month) { 
			let actualMonth = month - 1;
			let timeDistance = +new Date(year, month) - +new Date(year, actualMonth);
			return timeDistance / (1000 * 60 * 60 * 24);
		},
		// 获取某月1号是周几
		getFirstDateWeek(year, month) { 
		
			return new Date(year, month - 1, 1).getDay()
		},
		// 上月 年、月
		preMonth(year, month) { 
			if (month == 1) {
				return {
					year: --year,
					month: 12
				}
			} else {
				return {
					year: year,
					month: --month
				}
			}
		},
		// 下月 年、月
		nextMonth(year, month) { 
			if (month == 12) {
				return {
					year: ++year,
					month: 1
				}
			} else {
				return {
					year: year,
					month: ++month
				}
			}
		},
		// 获取当月数据，返回数组
		getCurrentArr(){ 
			let currentMonthDateLen = this.getDateLen(this.data.currentYear, this.data.currentMonth) // 获取当月天数
			let currentMonthDateArr = [] // 定义空数组
            let now = new Date()
            console.log(now.getDate(),'当日')
			if (currentMonthDateLen > 0) {
				for (let i = 1; i <= currentMonthDateLen; i++) {
					currentMonthDateArr.push({
						month: 'current', // 只是为了增加标识，区分上下月
						date: i,
						choosed:false,
                        year:this.data.currentYear,
                        currentMonth:this.data.currentMonth,
						week:new Date(`${this.data.currentYear}-${this.data.currentMonth}-${i}`).getDay()
					})
				}
			}
			this.setData({
				currentMonthDateLen
			})
			return currentMonthDateArr
		},
		// 获取当月中，上月多余数据，返回数组
		getPreArr(){ 
			let preMonthDateLen = this.getFirstDateWeek(this.data.currentYear, this.data.currentMonth) // 当月1号是周几 == 上月残余天数）
			console.log(preMonthDateLen, 'preMonthDateLen')
            let preMonthDateArr = [] // 定义空数组
			if (preMonthDateLen > 0) {
				let { year, month } = this.preMonth(this.data.currentYear, this.data.currentMonth) // 获取上月 年、月
				let date = this.getDateLen(year, month) // 获取上月天数
				for (let i = 0; i < preMonthDateLen; i++) {
					preMonthDateArr.unshift({ // 尾部追加
                        month: 'pre', // 只是为了增加标识，区分当、下月
						date: date,
						week:new Date(`${year}-${month}-${date}`).getDay()
					})
					date--
				}
			}
			console.log(this.data.currentMonth, '上月')
			this.setData({
				preMonthDateLen
			})
			return preMonthDateArr
		},
		// 获取当月中，下月多余数据，返回数组
		getNextArr() { 
			let nextMonthDateLen = 42 - this.data.preMonthDateLen - this.data.currentMonthDateLen // 下月多余天数
			let { year, month } = this.nextMonth(this.data.currentYear, this.data.currentMonth) // 获取下月 年、月
			console.log(year, 'yrar下月')
			console.log(month, 'yrar1下月')
			let nextMonthDateArr = [] // 定义空数组
			if (nextMonthDateLen > 0) {
				for (let i = 1; i <= nextMonthDateLen; i++) {
					nextMonthDateArr.push({
                        month: 'next',// 只是为了增加标识，区分当、上月
						date: i,
						week:new Date(`${year}-${month}-${i}`).getDay()
					})
				}
			}
			return nextMonthDateArr
		},
		// 整合当月所有数据
		getAllArr(){ 
			let preArr = this.getPreArr()
			let currentArr = this.getCurrentArr()
			let nextArr = this.getNextArr()
			let allArr = [...preArr, ...currentArr, ...nextArr]
			console.log(allArr, '综合')
			this.setData({
				allArr,
				newAllArr:allArr
			})
			if(this.data.multi){
				let sendObj = {
					currentYear: this.data.currentYear,
					currentMonth: this.data.currentMonth,
					allArr:allArr
				}
				this.triggerEvent('sendObj', sendObj)
			}else{
				let sendObj = {
					currentYear: this.data.currentYear,
					currentMonth: this.data.currentMonth,
					allArr:allArr
				}
				this.triggerEvent('sendObj', sendObj)
			}

			// console.log(sendObj)
		
		},
		// 点击 上月
		gotoPreMonth(){ 
			let { year, month } = this.preMonth(this.data.currentYear, this.data.currentMonth)
			this.setData({
				currentYear: year,
				currentMonth: month
			})
			this.getAllArr()
		},
		// 点击 下月
		gotoNextMonth() { 
			let { year, month } = this.nextMonth(this.data.currentYear, this.data.currentMonth)
			this.setData({
				currentYear: year,
				currentMonth: month
			})
			this.getAllArr()
		},
		moreSelectChange(e){
			const idx = e.currentTarget.dataset.idx
			const days = this.data.newAllArr
			const day = e.currentTarget.dataset.day
            const monthType= e.currentTarget.dataset.monthtype
			if(monthType!='current')return 
			days[idx].choosed = !days[idx].choosed
			const selectDay = {
                year: this.data.currentYear,
                month: this.data.currentMonth,
                day: day,
            }
			this.setData({
                selectDay: selectDay,
				newAllArr:days
            }, () => {
				console.log(this.data.newAllArr)
                this.triggerEvent("selectDay", this.data.selectDay)
            })
		},
        // 点击具体某天
        selectChange(e){
            console.log(e,'eee')
            const day = e.currentTarget.dataset.day
            const monthType= e.currentTarget.dataset.monthtype
            console.log(monthType, 'monthType')
            if(monthType!='current')return 
            console.log(day, 'year')
            console.log( this.data.currentYear, 'allArrallArr')
            const selectDay = {
                year: this.data.currentYear,
                month: this.data.currentMonth,
                day: day,
            }
            this.setData({
                selectDay: selectDay
            })
            console.log(selectDay, 'select')
        }
	}
})